package com.cn.qgx428.orderingmeal;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.myClass.Food;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.Shop;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SQLQueryListener;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private LinearLayout skip;  //跳转按钮
    private TextView skip_time;

    Boolean isSkip = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bmob.initialize(this,"1b925fe2ccbb402f4034a3e28c31ad93");
        init();
    }

    private void init() {
        skip = (LinearLayout) findViewById(R.id.main_skip);
        skip.setOnClickListener(this);
        skip_time = (TextView)findViewById(R.id.main_clock);


        countdownTimer = new MyCountdownTimer(5000,1000);
        countdownTimer.start();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //点击
                if (isSkip){}
                //用户未点击
                else {
                    userLogin();
                }
            }
        },5000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_skip:
                userLogin();
                break;
        }
    };

    private void userLogin() {
        isSkip = true;
        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        if ("1".equals(sp.getString("isLogined",null))){
            MyKeyInfo keyInfo = new MyKeyInfo();
            keyInfo.setUserName(sp.getString("userName",null));
            keyInfo.setUserID(sp.getString("userId",null));
            Intent intent = new Intent(MainActivity.this,Home.class);
            intent.putExtra("keyInfo",keyInfo);
            startActivity(intent);
            finish();
        }else {
            startActivity(new Intent(MainActivity.this,Login.class));
            finish();
        }
    }

    private Handler handler = new Handler();
    private MyCountdownTimer countdownTimer;

    protected class MyCountdownTimer extends CountDownTimer{

        /**
         * @param millisInFuture    计时总时间
         * @param countDownInterval 间隔时间
         */
        public MyCountdownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            skip_time.setText("跳过 ("+millisInFuture/1000+")");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //显示更改后的时钟信息
            skip_time.setText("跳过 ("+millisUntilFinished/1000+")");
        }

        @Override
        public void onFinish() {

        }
    }
}
